<template>
  <div class='container'>
    <el-card>

      <!-- 面包屑 S-->
      <div slot="header" v-if="!isShow">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{$route.query.name}}</el-breadcrumb-item>
          <el-breadcrumb-item>标签管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 面包屑 E-->

      <CardHeader :elColSpan="[18,6]">
        <!-- 头部左侧 S-->
        <template #left>
          <el-form label-width="80px">
            <el-row type="flex">
              <el-col :span="10">
                <el-form-item label="目录名称">
                  <el-input v-model="form.directoryName" size="small"/>
                </el-form-item>
              </el-col>
              <el-col :span="10">
                <el-form-item label="状态">
                  <el-select v-model="form.state" placeholder="请选择" size="small">
                    <el-option
                      v-for="item in status"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-button  size="small" @click="handleClear">清除</el-button>
                <el-button  size="small" type="primary" @click="handleSearch">搜索</el-button>
              </el-col>
            </el-row>
          </el-form>
        </template>
        <!-- 头部左侧 E-->

        <!-- 头部右侧 S-->
        <template #right>
          <!-- 返回按钮 S-->
          <el-button
            v-if="!isShow"
            type="text"
            icon="el-icon-back"
            @click="$router.back()"
          >返回学科</el-button>
          <!-- 返回按钮 E-->
            <el-button
            type="success"
            size="small"
            icon="el-icon-edit"
            @click="addDirectorys"
            >新增目录</el-button>
        </template>
        <!-- 头部右侧 S-->
      </CardHeader>

      <!-- alert警告 S -->
      <el-alert
        title="消息提示的文案"
        type="info"
        :closable="false"
        show-icon>
      </el-alert>
      <!-- alert警告 E -->

      <!-- table表格区域 S -->
       <el-table
        :data="tableData.items"
        style="width: 100%">
        <el-table-column
          label="序号"
          type="index"
          >
        </el-table-column>
        <el-table-column
          prop="subjectName"
          label="所属学科"
          >
        </el-table-column>
        <el-table-column
          prop="directoryName"
          label="目录名称">
        </el-table-column>
         <el-table-column
          prop="username"
          label="创建者"
          >
        </el-table-column>
        <el-table-column
          prop="addDate"
          label="创建日期"
          >
          <template slot-scope="{row}">
            {{row.addDate|parseTimeByString}}
          </template>
        </el-table-column>
        <el-table-column
          prop="totals"
          label="面试题数量">
        </el-table-column>
         <el-table-column
          prop="state"
          label="状态"
          >
          <template slot-scope="{row}">
            {{handleState(row.state)}}
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="操作"
          >
          <template slot-scope="{row}">
            <el-button type="text" @click="handleClick(row)">{{handleButtonState(row.state)}}</el-button>
            <el-button type="text" :disabled="!!row.state" @click="handleModify(row)">修改</el-button>
            <el-button type="text" :disabled="!!row.state" @click="handleDelete(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- table表格区域 E -->
      <!-- 分页器区域 S -->
      <Pagination
      :counts="counts"
      :page="{currentPage:1,pageSize:10}"
      @changePageSize="changePageSize"
      @changeCurrentPage="changeCurrentPage"
      ></Pagination>
      <!-- 分页器区域 E -->

      <!-- 新增目录弹框 S -->
      <DirectorysAdd
      :text="text"
      pageTitle="目录"
      ref="dirAdd"
      @success="loadDirectorysList()"
      ></DirectorysAdd>
      <!-- 新增目录弹框 E -->
    </el-card>
  </div>
</template>

<script>
import { status } from '@/api/hmmm/constants.js'
import { list as directorysList, changeState, remove } from '@/api/hmmm/directorys.js'
import DirectorysAdd from '../components/directorys-add.vue'
export default {
  name: 'Directorys',
  components: {
    DirectorysAdd
  },
  data () {
    return {
      form: {
        page: 1,
        pagesize: 10,
        subjectID: null, // 学科ID
        directoryName: null, // 目录名称
        state: null// 状态 1 开启 0 屏蔽
      },
      tableData: {},
      counts: 0,
      text: ''
    }
  },
  computed: {
    status: _ => status,
    isShow () {
      return !this.$route.query.id
    }
  },
  methods: {
    // 获取列表数据
    async loadDirectorysList (form) {
      const queryId = this.$route.query.id
      const newForm = form || { page: 1, pagesize: 10 }
      if (queryId) {
        this.form.subjectID = queryId - 0
      }
      for (var key in this.form) {
        if (this.form[key]) {
          newForm[key] = this.form[key]
        }
      }
      console.log(newForm)
      const { data } = await directorysList(newForm)
      this.tableData = data
      this.counts = data.counts
    },
    // 点击清除按钮
    handleClear () {
      this.form = {
        page: 1,
        pagesize: 10,
        // subjectID: '', // 学科ID
        directoryName: '', // 目录名称
        state: null// 状态 1 开启 0 屏蔽
      }
      this.loadDirectorysList()
    },
    // 点击搜索按钮
    handleSearch () {
      console.log(123)
      this.loadDirectorysList(this.form)
    },
    // 状态处理方法
    handleState (state) {
      return this.status.find(item => item.value === state).label
    },
    // 文字按钮状态处理方法
    handleButtonState (state) {
      return state === 0 ? '启用' : '禁用'
    },
    // 点击启用或禁用文字按钮时
    async handleClick (row) {
      row.state = row.state === 0 ? 1 : 0
      try {
        await changeState({ id: row.id, state: row.state })
        this.$message.success('操作成功')
      } catch (error) {

      }
    },
    // 点击删除按钮
    handleDelete (id) {
      this.$confirm('此操作将永久删除该目录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id })
        this.$message({
          type: 'success',
          message: '删除目录成功!'
        })
        this.loadDirectorysList(this.form)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 改变页码尺寸
    changePageSize (val) {
      this.form.pagesize = val
      this.loadDirectorysList(this.form)
    },
    // 改变当前页码
    changeCurrentPage (val) {
      this.form.page = val
      this.loadDirectorysList(this.form)
    },
    // 点击新增目录
    addDirectorys () {
      this.text = '新增'
      const obj = {}
      if (this.$route.query.id) {
        obj.subjectID = this.$route.query.id - 0
      }
      this.$refs.dirAdd.dialogFormV(obj, this.isShow)
    },
    // 点击修改
    handleModify (row) {
      this.text = '编辑'
      this.$refs.dirAdd.dialogFormV(row, this.isShow)
    }
  },
  created () {
    this.loadDirectorysList()
  }
}
</script>

<style scoped lang='less'></style>
